<?php echo $header; ?>
<div id="content">
<div class="breadcrumb">
  <?php foreach ($breadcrumbs as $breadcrumb) { ?>
  <?php echo $breadcrumb['separator']; ?><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a>
  <?php } ?>
</div>
<?php if ($error_warning) { ?>
<div class="warning"><?php echo $error_warning; ?></div>
<?php } ?>
<div class="box">
  <div class="heading">
    <h1><img src="view/image/module.png" alt="" /> <?php echo $heading_title; ?></h1>
    <div class="buttons"><a onclick="$('#form').submit();" class="button"><?php echo $button_save; ?></a><a onclick="location = '<?php echo $cancel; ?>';" class="button"><?php echo $button_cancel; ?></a></div>
  </div>
  <div class="content">
    <form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form">
	  <!-- start module -->
	  <table id="module" class="list">
        <thead>
          <tr>
			<td class="left"><?php echo $entry_image_category; ?></td>
			<td class="left"><?php echo $entry_image_size; ?></td>
			<td class="left"><?php echo $entry_category; ?></td>
			<td class="left"><?php echo $entry_image; ?></td>
			<td class="left"><?php echo $entry_limit; ?></td>      
		    <td class="left"><?php echo $entry_layout; ?></td>
            <td class="left"><?php echo $entry_position; ?></td>
            <td class="left"><?php echo $entry_store; ?></td>
            <td class="left"><?php echo $entry_status; ?></td>
            <td class="right"><?php echo $entry_sort_order; ?></td>
            <td></td>
          </tr>
        </thead>
        <?php $module_row = 0; ?>
        <?php foreach ($modules as $module) { ?>
        <tbody id="module-row<?php echo $module_row; ?>">
          <tr>
          	<td class="left"><div class="image"><img src="<?php echo $image[$module_row]['thumb']; ?>" alt="" id="thumb<?php echo $module_row; ?>" />
                  <input type="hidden" name="boss_featuredcategory_module[<?php echo $module_row; ?>][image]" value="<?php echo $image[$module_row]['image']; ?>" id="image<?php echo $module_row; ?>"  />
                  <br />
                  <a onclick="image_upload('image<?php echo $module_row; ?>', 'thumb<?php echo $module_row; ?>');"><?php echo $text_browse; ?></a>&nbsp;&nbsp;|&nbsp;&nbsp;<a onclick="$('#thumb<?php echo $module_row; ?>').attr('src', '<?php echo $no_image; ?>'); $('#image<?php echo $module_row; ?>').attr('value', '');"><?php echo $text_clear; ?></a></div></td>
          	<td class="left">
          		<input type="text" name="boss_featuredcategory_module[<?php echo $module_row;?>][image_width]" value="<?php echo (isset($module['image_width']) ? $module['image_width'] : '') ?>" size="3" /> <input type="text" name="boss_featuredcategory_module[<?php echo $module_row;?>][image_height]" value="<?php echo (isset($module['image_height']) ? $module['image_height'] : '') ?>" size="3" /> <?php if (isset($error_image_cate[$module_row])) { ?>
					<span class="error"><?php echo $error_image_cate[$module_row]; ?></span>
					<?php } ?></td>

			<td class="left"><select name="boss_featuredcategory_module[<?php echo $module_row; ?>][category_id]">
				<?php foreach ($categories as $category) { ?>
					<?php if($category['category_id'] == $module['category_id']){ ?>
						<option value="<?php echo $category['category_id']; ?>" selected="selected"><?php echo $category['name']; ?></option>
					<?php }else{ ?>
						<option value="<?php echo $category['category_id']; ?>"><?php echo $category['name']; ?></option>
					<?php } ?>
				<?php } ?>
			</select></td>
			<td class="left">
				<input type="text" name="boss_featuredcategory_module[<?php echo $module_row; ?>][width]" value="<?php echo $module['width']; ?>" size="3" />			
				<input type="text" name="boss_featuredcategory_module[<?php echo $module_row; ?>][height]" value="<?php echo $module['height']; ?>" size="3" />
				<?php if (isset($error_image[$module_row])) { ?>
				<span class="error"><?php echo $error_image[$module_row]; ?></span>
				<?php } ?></td>
			<td class="left"><input type="text" name="boss_featuredcategory_module[<?php echo $module_row; ?>][limit]" value="<?php echo $module['limit']; ?>" size="1" />
				<?php if (isset($error_limit[$module_row])) { ?>
				<span class="error"><?php echo $error_limit[$module_row]; ?></span>
				<?php } ?>
			</td>
            <td class="left"><select name="boss_featuredcategory_module[<?php echo $module_row; ?>][layout_id]">
               <?php foreach ($layouts as $layout) { ?>
					<?php if ($layout['layout_id'] == $module['layout_id']) { ?>
					<option value="<?php echo $layout['layout_id']; ?>" selected="selected"><?php echo $layout['name']; ?></option>
					<?php } else { ?>
					<option value="<?php echo $layout['layout_id']; ?>"><?php echo $layout['name']; ?></option>
					<?php } ?>
				<?php } ?>
			</select></td>
            <td class="left"><select name="boss_featuredcategory_module[<?php echo $module_row; ?>][position]">
                <?php if ($module['position'] == 'content_top') { ?>
                <option value="content_top" selected="selected"><?php echo $text_content_top; ?></option>
                <?php } else { ?>
                <option value="content_top"><?php echo $text_content_top; ?></option>
                <?php } ?>
                <?php if ($module['position'] == 'content_bottom') { ?>
                <option value="content_bottom" selected="selected"><?php echo $text_content_bottom; ?></option>
                <?php } else { ?>
                <option value="content_bottom"><?php echo $text_content_bottom; ?></option>
                <?php } ?>
              </select></td>
				<td><div class="scrollbox" style="width:200px;overflow: auto;">
				  <?php $class = 'even'; ?>
				  <div class="<?php echo $class; ?>">
					<?php if (isset($module['store_id']) && in_array(0, $module['store_id'])) { ?>
					<input type="checkbox" name="boss_featuredcategory_module[<?php echo $module_row;?>][store_id][]" value="0" checked="checked" />
					<?php } else { ?>
					<input type="checkbox" name="boss_featuredcategory_module[<?php echo $module_row;?>][store_id][]" value="0" />
					<?php } ?>
					<?php echo $text_default; ?>
				  </div>
				  <?php foreach ($stores as $store) { ?>
				  <?php $class = ($class == 'even' ? 'odd' : 'even'); ?>
				  <div class="<?php echo $class; ?>">
					<?php if (isset($module['store_id']) && in_array($store['store_id'], $module['store_id'])) { ?>
					<input type="checkbox" name="boss_featuredcategory_module[<?php echo $module_row;?>][store_id][]" value="<?php echo $store['store_id']; ?>" checked="checked" />
					<?php echo $store['name']; ?>
					<?php } else { ?>
					<input type="checkbox" name="boss_featuredcategory_module[<?php echo $module_row;?>][store_id][]" value="<?php echo $store['store_id']; ?>" />
					<?php echo $store['name']; ?>
					<?php } ?>
				  </div>
				  <?php } ?>
				</div></td>


            <td class="left"><select name="boss_featuredcategory_module[<?php echo $module_row; ?>][status]">
                <?php if ($module['status']) { ?>
                <option value="1" selected="selected"><?php echo $text_enabled; ?></option>
                <option value="0"><?php echo $text_disabled; ?></option>
                <?php } else { ?>
                <option value="1"><?php echo $text_enabled; ?></option>
                <option value="0" selected="selected"><?php echo $text_disabled; ?></option>
                <?php } ?>
              </select></td>
            <td class="right"><input type="text" name="boss_featuredcategory_module[<?php echo $module_row; ?>][sort_order]" value="<?php echo $module['sort_order']; ?>" size="3" /></td>
            <td class="left"><a onclick="$('#module-row<?php echo $module_row; ?>').remove();" class="button"><?php echo $button_remove; ?></a></td>
          </tr>
        </tbody>
        <?php $module_row++; ?>
        <?php } ?>
        <tfoot>
          <tr>
            <td colspan="10"></td>
            <td class="left"><a onclick="addModule();" class="button"><?php echo $button_add_module; ?></a></td>
          </tr>
        </tfoot>
      </table>
	  <!-- end module -->
    </form>
  </div>
</div>
<script type="text/javascript"><!--
  var module_row = <?php echo $module_row; ?>;

  function addModule() {	
	html  = '<tbody id="module-row' + module_row + '">';
	html += '  <tr>';	
	html += '    <td class="left"><div class="image"><img src="<?php echo $no_image; ?>" alt="" id="thumb' + module_row + '" /></div>';	
	html += '    <input type="hidden" name="boss_featuredcategory_module[' + module_row + '][image]" value="" id="image'+ module_row + '"  /> <a onclick="image_upload(\'image' + module_row + '\', \'thumb' + module_row + '\');"><?php echo $text_browse; ?></a>&nbsp;&nbsp;|&nbsp;&nbsp;<a onclick="$(\'#thumb' + module_row + '\').attr(\'src\', \'<?php echo $no_image; ?>\'); $(\'#image'+ module_row + '\').attr(\'value\', \'\');"><?php echo $text_clear; ?></a></div>';
	html += '    </td>';	
		
	html += '    <td class="left"><input type="text" name="boss_featuredcategory_module[' + module_row + '][image_width]" value="20" size="3" /> <input type="text" name="boss_featuredcategory_module[' + module_row + '][image_height]" value="20" size="3" /></td>';	
	html += '	 <td class="left"><select name="boss_featuredcategory_module['+ module_row + '][category_id]">';
	<?php foreach ($categories as $category) { ?>
	html += '	   <option value="<?php echo $category['category_id']; ?>"><?php echo $category['name']; ?></option>';
	<?php } ?>
	html += '	 </select></td>';
	html += '    <td class="left"><input type="text" name="boss_featuredcategory_module[' + module_row + '][width]" value="170" size="3" /> <input type="text" name="boss_featuredcategory_module[' + module_row + '][height]" value="170" size="3" /></td>';	
	html += '    <td class="left"><input type="text" name="boss_featuredcategory_module[' + module_row + '][limit]" value="20" size="1" /></td>';
		
	html += '    <td class="left"><select name="boss_featuredcategory_module[' + module_row + '][layout_id]">';
	<?php foreach ($layouts as $layout) { ?>
	html += '      <option value="<?php echo $layout['layout_id']; ?>"><?php echo addslashes($layout['name']); ?></option>';
	<?php } ?>
	html += '    </select></td>';	
	html += '    <td class="left"><select name="boss_featuredcategory_module[' + module_row + '][position]">';
	html += '      <option value="content_top"><?php echo $text_content_top; ?></option>';
	html += '      <option value="content_bottom"><?php echo $text_content_bottom; ?></option>';
	html += '    </select></td>';

	html += '    <td><div class="scrollbox" style="width:200px;overflow: auto;">';
	var even_old = 'even';
	html += '     <div class="'+ even_old +'"><input type="checkbox" name="boss_featuredcategory_module['+module_row+'][store_id][]" value="0" /><?php echo $text_default; ?></div>';
	<?php foreach ($stores as $store) { ?>
 	if(even_old == 'even'){ even_old = 'odd'}else{even_old = 'even' }
	html += '<div class="'+even_old+'"><input type="checkbox" name="boss_featuredcategory_module['+module_row+'][store_id][]" value="<?php echo $store['store_id']; ?>" /><?php echo $store['name']; ?></div>';
	<?php } ?>
	html += '    </div></td>';

	html += '    <td class="left"><select name="boss_featuredcategory_module[' + module_row + '][status]">';
    html += '      <option value="1" selected="selected"><?php echo $text_enabled; ?></option>';
    html += '      <option value="0"><?php echo $text_disabled; ?></option>';
    html += '    </select></td>';
	html += '    <td class="right"><input type="text" name="boss_featuredcategory_module[' + module_row + '][sort_order]" value="" size="3" /></td>';
	html += '    <td class="left"><a onclick="$(\'#module-row' + module_row + '\').remove();" class="button"><?php echo $button_remove; ?></a></td>';
	html += '  </tr>';
	html += '</tbody>';
	
	$('#module tfoot').before(html);
	
	module_row++;
  }
//--></script> 
<script type="text/javascript"><!--
function image_upload(field, thumb) {
	$('#dialog').remove();
	
	$('#content').prepend('<div id="dialog" style="padding: 3px 0px 0px 0px;"><iframe src="index.php?route=common/filemanager&token=<?php echo $token; ?>&field=' + encodeURIComponent(field) + '" style="padding:0; margin: 0; display: block; width: 100%; height: 100%;" frameborder="no" scrolling="auto"></iframe></div>');
	
	$('#dialog').dialog({
		title: '<?php echo $text_image_manager; ?>',
		close: function (event, ui) {
			if ($('#' + field).attr('value')) {
				$.ajax({
					url: 'index.php?route=common/filemanager/image&token=<?php echo $token; ?>&image=' + encodeURIComponent($('#' + field).attr('value')),
					dataType: 'text',
					success: function(data) {
						$('#' + thumb).replaceWith('<img src="' + data + '" alt="" id="' + thumb + '" />');
					}
				});
			}
		},	
		bgiframe: false,
		width: 700,
		height: 400,
		resizable: false,
		modal: false
	});
};
//--></script> 
<?php echo $footer; ?>